<script>
import axios from "axios";

export default {
  name: "FriendInfo",
  props:['id'],
  data() {
    return {
      user: {
        nickname: "",
        avatar: "",
        gender: "",
        signature: ""
      },
      fas:0,
      follow:0,
      photo:0,
      dynamic:0,
      message:0,
      loading:false
    }
  },
  methods: {
    get() {
      axios.get(`/api/user/${this.id}`)
          .then(res => {
            if(res.data.code === 0){
              this.loading = false

              this.user = res.data.data
            }else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    },
    getFas() {
      axios.get(`/api/friends/fans/${this.id}`)
          .then(res => this.fans = res.data)
          .catch(() => this.fans = 0)

    },
    getFollow(){
      axios.get(`/api/friends/follow/${this.id}`)
          .then(res => this.follow = res.data)
          .catch(() => this.follow = 0)
    },
    getPhoto() {
      axios.get(`/api/photo/count/${this.id}`)
          .then(res => this.photo = res.data)
          .catch(() => this.photo = 0)
    },
    getDynamic() {
      axios.get(`/api/dynamic/count/${this.id}`)
          .then(res => this.dynamic = res.data)
          .catch(() => this.dynamic = 0)
    },
    getMessage() {
      axios.get(`/api/message/count/${this.id}`)
          .then(res => this.message = res.data)
          .catch(() => this.message = 0)
    },
    setFollow() {
      axios.get(`/api/friends/add/${this.id}`)
          .then(() => {
            let date = new Date()
            this.$bus.$emit("sendMessage", {
              uid: this.id,
              time: `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, '0')}${String(date.getDate()).padStart(2, '0')}${String(date.getHours()).padStart(2, '0')}${String(date.getMinutes()).padStart(2, '0')}`,
              context: `${this.$store.state.user.nickname}关注了你`
            })
            let formData = new FormData()
            formData.set("fid", this.id)
            formData.set("content", `${this.$store.state.user.nickname}关注了你`)
            axios.post("/api/chat/add", formData)
            this.$message.success("已关注对方")
            this.get()
          })
          .catch(err => this.$message.error(err))
    },
  },
  watch:{
    id:{
      immediate:true,
      handler(value) {
        if(value !== '') {
          this.loading = true
          this.get()
          this.getPhoto()
          this.getFollow()
          this.getDynamic()
          this.getFas()
          this.getMessage()
        }

      }
    }
  },
  beforeDestroy() {
    this.user={}
    this.fas = 0
    this.follow = 0
    this.dynamic = 0
    this.photo = 0
    this.message = 0
  }
}
</script>

<template>
  <div class="f_box" v-loading="loading">
    <div class="f_left">
      <div class="md_img">
        <el-avatar shape="square" alt="" :src="user.avatar"></el-avatar>
      </div>
      <div class="zl_hy" style="display: flex;justify-content: center">
        粉丝:<span style="color: black;margin-right: 10px;margin-left: 3px">{{ fas }}</span>
        关注:<span style="color: black;margin-left: 3px">{{ follow }}</span>
      </div>
      <div class="ll_box">
        <div class="ll_item">
          <div style="height: 40px;line-height: 40px;font-size: 18px">照片</div>
          <div style="height: 20px;line-height: 20px" ><a @click="$emit('set',1)">{{ photo }}</a></div>
        </div>
        <div class="line"></div>
        <div class="ll_item">
          <div style="height: 40px;line-height: 40px;font-size: 18px">动态</div>
          <div style="height: 20px;line-height: 20px"><a @click="$emit('set',2)">{{ dynamic }}</a></div>
        </div>
        <div class="line"></div>
        <div class="ll_item">
          <div style="height: 40px;line-height: 40px;font-size: 18px">留言</div>
          <div style="height: 20px;line-height: 20px"><a @click="$emit('set',3)">{{ message }}</a></div>
        </div>
      </div>
      <div style="width: 80px;margin: 0 auto;">
        <button v-if="!user.isFriend" class="ll_btn" @click="setFollow">关注</button>

      </div>

    </div>


    <div class="f_right">
      <div class="md_cont" v-if="user.remark !== ''">
        <div class="md_text" >备注</div>
        <div class="md_email">
          {{ user.remark }}
          <i :class="user.gender==='男'?'el-icon-male':'el-icon-female'"></i>
        </div>
      </div>
      <div class="md_cont">
        <div class="md_text">昵称</div>
        <div class="md_email">
          {{ user.nickname }}
          <i :class="user.gender==='男'?'el-icon-male':'el-icon-female'" v-if="user.remark === ''"></i>

        </div>
      </div>
      <div class="md_cont">
        <div class="md_text">账号</div>
        <div class="md_email">
          {{ user.username }}
        </div>
      </div>
      <div class="md_cont">
        <div class="md_text">邮箱</div>
        <div class="md_email">
          {{ user.email }}
        </div>
      </div>
      <div class="md_cont" v-if="user.signature !== ''">
        <div class="md_text">签名</div>
        <div class="md_email">
          {{ user.signature }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.f_box {

}

.f_left {
  width: 30%;
  float: left;
}

.f_right {
  width: 70%;
  float: right;
}

.md_img {
  width: 90px;
  margin: 50px auto 20px;

  .el-avatar {
    height: 90px;
    width: 90px;
  }
}

.ll_box {
  border-radius: 1px;
  margin: 20px auto;
  height: 70px;
  width: 212px;
  background-color: #f1f2f5;
  display: flex;
  justify-content: space-between;
}

.ll_item {
  height: 70px;
  width: 70px;
  text-align: center;
}

.ll_item a:hover {
  text-decoration: underline;
}

.ll_btn {
  width: 80px;
  cursor: pointer;
  height: 40px;
  background-color: #ffffff;
  border-radius: 10px;
  border:1px solid #409EFF;
}

.ll_btn:hover {
  background-color: #409EFF;
  color: #ffffff;
}

.line {
  width: 1px;
  background-color: #cdcdcd;
  margin-top: 5px;
  height: 60px;
}

.md_cont {
  font-size: 17px;
  font-weight: bold;
  color: #8a8a8a;
  width: 360px;
  margin: 20px 10px;
  display: flex;
}

.md_text {
  width: 60px;
  line-height: 40px;
  margin-right: 20px;
  text-align: justify;
  text-align-last: justify;
}

.md_email {
  line-height: 40px;
  color: #222327;
}

.md_email a {
  text-decoration: underline;
  color: #409EFF;
}

.md_btn_box {
  margin: 0 auto;
  height: 50px;
  width: 100px;
  padding-top: 20px;
}
</style>